package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/11/29.
 */
class HomeViewModel : BaseViewModel() {

    private var loadHomePageFresh = MutableLiveData<FreshBean>()

    private var loadHomePageResult: LiveData<ViewDataBean<ResultBean<HomePageBean>>>? = null

    fun loadHomePage(): LiveData<ViewDataBean<ResultBean<HomePageBean>>>? {
        if (loadHomePageResult == null) {
            loadHomePageResult = Transformations.switchMap(loadHomePageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().loadHomePage(it.map)
                } else {
                    null
                }
            }
        }
        return loadHomePageResult!!
    }

    fun refreshLoadHomePage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) loadHomePageFresh.value = FreshBean(map, isFresh)
    }

    private var getGroupGoodsFresh = MutableLiveData<FreshBean>()

    private var getGroupGoodsResult: LiveData<ViewDataBean<ResultBean<GroupGoodBean>>>? = null

    fun getGroupGoods(): LiveData<ViewDataBean<ResultBean<GroupGoodBean>>>? {
        if (getGroupGoodsResult == null) {
            getGroupGoodsResult = Transformations.switchMap(getGroupGoodsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getGroupGoods(it.map)
                } else {
                    null
                }
            }
        }
        return getGroupGoodsResult!!
    }

    fun refreshGetGroupGoods(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getGroupGoodsFresh.value = FreshBean(map, isFresh)
    }

    private var getEarnDataFresh = MutableLiveData<FreshBean>()

    private var getEarnDataResult: LiveData<ViewDataBean<ResultBean<EarnDataBean>>>? = null

    fun getEarnData(): LiveData<ViewDataBean<ResultBean<EarnDataBean>>>? {
        if (getEarnDataResult == null) {
            getEarnDataResult = Transformations.switchMap(getEarnDataFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getEarnData(it.map)
                } else {
                    null
                }
            }
        }
        return getEarnDataResult!!
    }

    fun refreshGetEarnData(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getEarnDataFresh.value = FreshBean(map, isFresh)
    }

    private var optionalSearchFresh = MutableLiveData<FreshBean>()

    private var optionalSearchResult: LiveData<ViewDataBean<ResultBean<OptionalGoodBean>>>? = null

    fun optionalSearch(): LiveData<ViewDataBean<ResultBean<OptionalGoodBean>>>? {
        if (optionalSearchResult == null) {
            optionalSearchResult = Transformations.switchMap(optionalSearchFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().optionalSearch(it.map)
                } else {
                    null
                }
            }
        }
        return optionalSearchResult!!
    }

    fun refreshOptionalSearch(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) optionalSearchFresh.value = FreshBean(map, isFresh)
    }

    private var shopOptionalSearchFresh = MutableLiveData<FreshBean>()

    private var shopOptionalSearchResult: LiveData<ViewDataBean<ResultBean<OptionalGoodBean>>>? = null

    fun shopOptionalSearch(): LiveData<ViewDataBean<ResultBean<OptionalGoodBean>>>? {
        if (shopOptionalSearchResult == null) {
            shopOptionalSearchResult = Transformations.switchMap(shopOptionalSearchFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().shopOptionalSearch(it.map)
                } else {
                    null
                }
            }
        }
        return shopOptionalSearchResult!!
    }

    fun refreshShopOptionalSearch(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) shopOptionalSearchFresh.value = FreshBean(map, isFresh)
    }

    private var getTaoKouLingFresh = MutableLiveData<FreshBean>()

    private var getTaoKouLingResult: LiveData<ViewDataBean<RemoteResultBean<String>>>? = null

    fun getTaoKouLing(): LiveData<ViewDataBean<RemoteResultBean<String>>>? {
        if (getTaoKouLingResult == null) {
            getTaoKouLingResult = Transformations.switchMap(getTaoKouLingFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getTaoKouLing(it.map)
                } else {
                    null
                }
            }
        }
        return getTaoKouLingResult!!
    }

    fun freshGetTaoKouLing(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getTaoKouLingFresh.value = FreshBean(map, isFresh)
    }

    private var saveShareItemInfoFresh = MutableLiveData<FreshBean>()

    private var saveShareItemInfoResult: LiveData<ViewDataBean<RemoteResultBean<String>>>? = null

    fun saveShareItemInfo(): LiveData<ViewDataBean<RemoteResultBean<String>>>? {
        if (saveShareItemInfoResult == null) {
            saveShareItemInfoResult = Transformations.switchMap(saveShareItemInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().saveShareItemInfo(it.map)
                } else {
                    null
                }
            }
        }
        return saveShareItemInfoResult!!
    }

    fun freshSaveShareItemInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) saveShareItemInfoFresh.value = FreshBean(map, isFresh)
    }

    private var addCollectFresh = MutableLiveData<FreshBean>()

    private var addCollectResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun addCollect(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (addCollectResult == null) {
            addCollectResult = Transformations.switchMap(addCollectFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().addCollect(it.map)
                } else {
                    null
                }
            }
        }
        return addCollectResult!!
    }

    fun freshAddCollect(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) addCollectFresh.value = FreshBean(map, isFresh)
    }

    private var isGoodCollectFresh = MutableLiveData<FreshBean>()

    private var isGoodCollectResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun isGoodCollect(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (isGoodCollectResult == null) {
            isGoodCollectResult = Transformations.switchMap(isGoodCollectFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().isGoodCollect(it.map)
                } else {
                    null
                }
            }
        }
        return isGoodCollectResult!!
    }

    fun freshIsGoodCollect(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) isGoodCollectFresh.value = FreshBean(map, isFresh)
    }

    private var getHotKeywordFresh = MutableLiveData<FreshBean>()

    private var getHotKeywordResult: LiveData<ViewDataBean<RemoteResultBean<List<String>>>>? = null

    fun getHotKeyword(): LiveData<ViewDataBean<RemoteResultBean<List<String>>>>? {
        if (getHotKeywordResult == null) {
            getHotKeywordResult = Transformations.switchMap(getHotKeywordFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getHotKeyword(it.map)
                } else {
                    null
                }
            }
        }
        return getHotKeywordResult!!
    }

    fun freshGetHotKeyword(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getHotKeywordFresh.value = FreshBean(map, isFresh)
    }

    private var getTaobaoTablePageFresh = MutableLiveData<FreshBean>()

    private var getTaobaoTablePageResult: LiveData<ViewDataBean<RemoteResultBean<TaobaoTablePageBean>>>? = null

    fun getTaobaoTablePage(): LiveData<ViewDataBean<RemoteResultBean<TaobaoTablePageBean>>>? {
        if (getTaobaoTablePageResult == null) {
            getTaobaoTablePageResult = Transformations.switchMap(getTaobaoTablePageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getTaobaoTablePage(it.map)
                } else {
                    null
                }
            }
        }
        return getTaobaoTablePageResult!!
    }

    fun freshGetTaobaoTablePage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getTaobaoTablePageFresh.value = FreshBean(map, isFresh)
    }
}